package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IChannelDao;
import com.magicode.cms.dao.IFilmBoxOfficeDao;
import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.FilmBoxOffice;
import com.magicode.cms.service.IChannelService;
import com.magicode.cms.service.IFilmBoxOfficeService;

@Service("serviceFilmBoxOffice")
public class FilmBoxOfficeServiceImpl extends BaseServiceImpl<FilmBoxOffice,IFilmBoxOfficeDao> implements IFilmBoxOfficeService,UtilPath{

	@Autowired
	@Qualifier("daoFilmBoxOffice")
	private IFilmBoxOfficeDao filmBoxOfficeDao;
	public IFilmBoxOfficeDao getFilmBoxOfficeDao() {
		return filmBoxOfficeDao;
	}

	public void setFilmBoxOfficeDao(IFilmBoxOfficeDao filmBoxOfficeDao) {
		this.filmBoxOfficeDao = filmBoxOfficeDao;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "filmName");
		map.put(json_title, "电影片名");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "statisticsDate");
		map.put(json_title, "年月日");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "currentBoxOffice");
		map.put(json_title, "日票房");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "allBoxOffice");
		map.put(json_title, "累计票房");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "showDay");
		map.put(json_title, "上映天数");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "source");
		map.put(json_title, "来源");
		map.put("width", 10);
		beaninfo.add(map);
		
		return beaninfo;
	}
	
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("filmName", ""+entity_alias_name+".filmName as filmName ");
			gridsel.put("statisticsDate", ""+entity_alias_name+".statisticsDate as statisticsDate ");
			gridsel.put("currentBoxOffice", ""+entity_alias_name+".currentBoxOffice as currentBoxOffice ");
			gridsel.put("allBoxOffice", ""+entity_alias_name+".allBoxOffice as allBoxOffice ");
			gridsel.put("showDay", ""+entity_alias_name+".showDay as showDay ");
			gridsel.put("source", " s.title as source ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.FilmBoxOffice.class.getName()+" t"
//				+ " left outer join t.program as p "
				+ " left outer join t.source as s"
				+ " where 1 = 1 ";
		if(null!=map && !map.isEmpty()){
			/*if(map.containsKey(field_article_id)){
				hql = hql+" ";
			}*/
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}

			if(map.containsKey("filmName")){
				hql = hql+" and "+entity_alias_name+".filmName = '"+map.get("filmName")+"'";
			}
			if(map.containsKey("date")){
				hql = hql+" and "+entity_alias_name+".statisticsDate = '"+map.get("date")+"'";
			}
			System.out.println(hql);
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("filmName", ""+entity_alias_name+".filmName as filmName ");
			gridsel.put("statisticsDate", ""+entity_alias_name+".statisticsDate as statisticsDate ");
			gridsel.put("currentBoxOffice", ""+entity_alias_name+".currentBoxOffice as currentBoxOffice ");
			gridsel.put("allBoxOffice", ""+entity_alias_name+".allBoxOffice as allBoxOffice ");
			gridsel.put("showDay", ""+entity_alias_name+".showDay as showDay ");
			gridsel.put("source", " s.title as source ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}

	@Override
	public IFilmBoxOfficeDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return filmBoxOfficeDao;
	}

	@Override
	public FilmBoxOffice getFilmBoxOffice(String filmName, String date)
			throws Exception {
		Map<String,String> prams = new HashMap<String,String>();
		prams.put("filmName", filmName);
		prams.put("date", date);
		List<FilmBoxOffice> films =filmBoxOfficeDao.queryDataList("select t "+this.getHql(prams), 0, 0);
		return films.size()>0?films.get(0):null;
	}

	

}
